package org.dromara.quick.chat.service.impl;

import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.utils.IdGenerator;
import org.dromara.common.model.enums.im.AuditStatusEnums;
import org.dromara.common.mongo.wrapper.WrapperCriteria;
import org.dromara.common.mongo.wrapper.WrapperUpdate;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.quick.chat.domain.ImSocialComment;
import org.dromara.quick.chat.domain.ImSocialPost;
import org.dromara.quick.chat.model.bo.create.ImSocialCommentBO;
import org.dromara.quick.chat.model.bo.query.ImSocialCommentQueryBO;
import org.dromara.quick.chat.model.vo.ImSocialCommentVO;
import org.dromara.quick.chat.service.ImSocialCommentService;
import org.dromara.system.api.RemoteUserService;
import org.dromara.system.api.domain.vo.RemoteUserBasicVO;
import org.dromara.system.api.model.LoginUser;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Stars
* @description 针对表【im_social_comment(社交软件中的评论表，用于存储用户对各种主题的评论信息)】的数据库操作Service实现
* @createDate 2025-03-04 22:33:21
*/
@Service
@RequiredArgsConstructor
public class ImSocialCommentServiceImpl implements ImSocialCommentService{

    private final MongoTemplate mongoTemplate;

    @DubboReference
    private final RemoteUserService remoteUserService;

    private final Converter converter;

    @Override
    public void commentLike(Long commentId) {
        WrapperCriteria<ImSocialComment> wrapperCriteria = new WrapperCriteria<ImSocialComment>().and(ImSocialComment::getId).is(commentId);
        mongoTemplate.updateFirst(Query.query(wrapperCriteria), new WrapperUpdate<ImSocialComment>().inc(ImSocialComment::getLikeCount,1), ImSocialComment.class);
    }

    @Override
    public void addComment(ImSocialCommentBO imSocialCommentBO) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<RemoteUserBasicVO> remoteUserBasicVOS = remoteUserService.selectListUserBasicByIds(List.of(loginUser.getUserId()));
        ImSocialComment imSocialComment = new ImSocialComment();
        imSocialComment.setId(IdGenerator.getId());
        imSocialComment.setUserId(loginUser.getUserId());
        imSocialComment.setType(imSocialCommentBO.getType());
        imSocialComment.setCommentSubjectId(imSocialCommentBO.getCommentSubjectId());
        imSocialComment.setIpLocation(imSocialCommentBO.getIpLocation());
        imSocialComment.setCommenterNickname(loginUser.getNickname());
        imSocialComment.setCommentType(imSocialCommentBO.getCommentType());
        imSocialComment.setCommentContent(imSocialCommentBO.getCommentContent());
        imSocialComment.setLikeCount(0);
        imSocialComment.setReplyToNickname(imSocialCommentBO.getReplyToNickname());
        imSocialComment.setReplyToId(imSocialCommentBO.getReplyToId());
        imSocialComment.setReplyToCommentId(imSocialCommentBO.getReplyToCommentId());
        imSocialComment.setReviewStatus(AuditStatusEnums.UNAUDITED.getCode());
        imSocialComment.setCreateTime(new Date());
        if (CollectionUtils.isNotEmpty(remoteUserBasicVOS)){
            imSocialComment.setCommenterAvatar(remoteUserBasicVOS.get(0).getAvatar());
        }
        mongoTemplate.save(imSocialComment);

        // 对应动态的评论数量需要添加一
        WrapperCriteria<ImSocialPost> wrapperCriteria = new WrapperCriteria<ImSocialPost>().and(ImSocialPost::getId).is(imSocialCommentBO.getCommentSubjectId());
        mongoTemplate.updateFirst(Query.query(wrapperCriteria), new WrapperUpdate<ImSocialPost>().inc(ImSocialPost::getCommentCount,1), ImSocialPost.class);
    }

    @Override
    public List<ImSocialCommentVO> list(ImSocialCommentQueryBO imSocialCommentQueryBO) {
        WrapperCriteria<ImSocialComment> wrapperCriteria = new WrapperCriteria<ImSocialComment>()
            .and(ImSocialComment::getCommentSubjectId).is(imSocialCommentQueryBO.getSocialPostId())
            .and(ImSocialComment::getReplyToCommentId).isNull();
        List<ImSocialComment> imSocialComments = mongoTemplate.find(Query.query(wrapperCriteria)
                .with(Sort.by(Sort.Direction.DESC,"likeCount","createTime"))
            .skip(imSocialCommentQueryBO.getFirstNum() - 1)
            .limit(imSocialCommentQueryBO.getPageSize()), ImSocialComment.class);
        if (CollectionUtils.isEmpty(imSocialComments)) {
            return Collections.emptyList();
        }
        List<ImSocialCommentVO> imSocialCommentList = converter.convert(imSocialComments, ImSocialCommentVO.class);
        // 获取所有的评论id
        List<Long> ids = imSocialComments.stream().map(ImSocialComment::getId).collect(Collectors.toList());
        // 查询子评论
        List<ImSocialComment> socialComments = mongoTemplate.find(Query.query(new WrapperCriteria<ImSocialComment>().and(ImSocialComment::getReplyToCommentId).in(ids)).with(Sort.by(Sort.Direction.DESC,"likeCount","createTime")), ImSocialComment.class);
        List<ImSocialCommentVO> imSocialCommentVOS = converter.convert(socialComments, ImSocialCommentVO.class);
        Map<Long, List<ImSocialCommentVO>> imSocialCommentVOMap = imSocialCommentVOS.stream()
            .collect(Collectors.groupingBy (ImSocialCommentVO::getReplyToCommentId));
        for (ImSocialCommentVO imSocialCommentVO : imSocialCommentList) {
            imSocialCommentVO.setChildren(imSocialCommentVOMap.get(imSocialCommentVO.getId()));
        }
        return imSocialCommentList;
    }
}




